<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="mat-content tb-rulechain flex flex-1 flex-col" tb-fullscreen [fullscreen]="isFullscreen" [tb-hotkeys]="hotKeys"
     [cheatSheet]="cheatSheetComponent">
  <tb-hotkeys-cheatsheet #cheatSheetComponent></tb-hotkeys-cheatsheet>
  <section class="tb-rulechain-container flex flex-1 flex-col">
    <div class="tb-rulechain-layout flex flex-1 flex-row">
      <mat-drawer-container class="size-full">
        <mat-drawer class="tb-rulechain-library mat-elevation-z4 flex flex-col"
                    disableClose="true"
                    mode="side"
                    #drawer
                    opened
                    position="start">
          <mat-toolbar color="primary" class="tb-dark">
            <tb-rule-chain-select
              class="flex-1"
              *ngIf="!isImport"
              [ruleChainType]="ruleChainType"
              [disabled]="isDirtyValue"
              [(ngModel)]="ruleChain"
              (ngModelChange)="currentRuleChainIdChanged(ruleChain.id?.id)">
            </tb-rule-chain-select>
          </mat-toolbar>
          <mat-toolbar>
            <div class="mat-toolbar-tools">
              <mat-form-field class="tb-appearance-transparent flex-1">
                <button mat-icon-button matPrefix class="tb-small"
                        matTooltip="{{'rulenode.search' | translate}}"
                        matTooltipPosition="above">
                  <mat-icon>search</mat-icon>
                </button>
                <input matInput
                       [formControl]="ruleNodeTypeSearch"
                       placeholder="{{'rulenode.search' | translate}}"/>
                <button mat-icon-button matSuffix class="tb-small"
                        [class.!hidden]="ruleNodeTypeSearch.value === ''"
                        (click)="ruleNodeTypeSearch.reset()"
                        matTooltip="{{'action.clear-search' | translate}}"
                        matTooltipPosition="above">
                  <mat-icon>close</mat-icon>
                </button>
              </mat-form-field>
            </div>
          </mat-toolbar>
          <div class="tb-rulechain-library-panel-group">
            <mat-expansion-panel #ruleNodeTypeExpansionPanels
                                 class="mat-elevation-z2"
                                 [expanded]="true" *ngFor="let ruleNodeType of ruleNodeTypesLibraryArray">
              <mat-expansion-panel-header expandedHeight="48px"
                                          (mouseenter)="typeHeaderMouseEnter($event, ruleNodeType)"
                                          (mouseleave)="destroyTooltips()">
                <mat-panel-title>
                  <mat-icon>{{ ruleNodeTypeDescriptorsMap.get(ruleNodeType).icon }}</mat-icon>
                  <div class="tb-panel-title" translate>{{ ruleNodeTypeDescriptorsMap.get(ruleNodeType).name }}</div>
                </mat-panel-title>
              </mat-expansion-panel-header>
              <fc-canvas id="tb-rulchain-{{ruleNodeType}}"
                         [model]="ruleNodeTypesModel[ruleNodeType].model"
                         [selectedObjects]="ruleNodeTypesModel[ruleNodeType].selectedObjects"
                         [automaticResize]="false"
                         fitModelSizeByDefault
                         [userCallbacks]="nodeLibCallbacks"
                         [nodeWidth]="170"
                         [nodeHeight]="50"
                         [dropTargetId]="'tb-rulchain-canvas'">
              </fc-canvas>
            </mat-expansion-panel>
          </div>
        </mat-drawer>
        <mat-drawer class="tb-details-drawer"
                    [opened]="isEditingRuleNode || isEditingRuleNodeLink"
                    (closed)="onDetailsDrawerClosed()"
                    mode="over"
                    position="end">
          <tb-details-panel *ngIf="editingRuleNode" class="flex-1"
                            headerTitle="{{editingRuleNode.name}}"
                            headerSubtitle="{{
                              (ruleNodeTypeDescriptorsMap.get(editingRuleNode.component.type).name | translate)
                              + ' - ' + editingRuleNode.component.name
                            }}"
                            [isReadOnly]="selectedRuleNodeTabIndex > 0"
                            [isAlwaysEdit]="true"
                            (closeDetails)="onEditRuleNodeClosed()"
                            (toggleDetailsEditMode)="onRevertRuleNodeEdit()"
                            (applyDetails)="saveRuleNode()"
                            [theForm]="tbRuleNode.ruleNodeFormGroup">
            <div class="details-buttons">
              <div [tb-help]="helpLinkIdForRuleNodeType()"></div>
            </div>
            <mat-tab-group mat-stretch-tabs="false" class="tb-absolute-fill tb-rulenode-details flex-1" [(selectedIndex)]="selectedRuleNodeTabIndex">
              <mat-tab label="{{ 'rulenode.details' | translate }}">
                <tb-rule-node #tbRuleNode
                              [ruleNode]="editingRuleNode"
                              [ruleChainId]="ruleChain.id?.id"
                              [ruleChainType]="ruleChainType"
                              (initRuleNode)="onRuleNodeInit()"
                              (changeScript)="switchToFirstTab()">
                </tb-rule-node>
              </mat-tab>
              <mat-tab *ngIf="editingRuleNode.ruleNodeId" label="{{ 'rulenode.events' | translate }}" #eventsTab="matTab">
                <tb-event-table [debugEventTypes]="[debugEventTypes.DEBUG_RULE_NODE]"
                                [defaultEventType]="debugEventTypes.DEBUG_RULE_NODE"
                                [active]="eventsTab.isActive"
                                [tenantId]="ruleChain.tenantId.id"
                                [entityId]="editingRuleNode.ruleNodeId"
                                [functionTestButtonLabel]="ruleNodeTestButtonLabel"
                                (debugEventSelected)="onDebugEventSelected($event)">
                </tb-event-table>
              </mat-tab>
              <mat-tab label="{{ 'rulenode.help' | translate }}">
                <div class="tb-rule-node-help">
                  <div id="tb-node-content" class="mat-padding flex flex-col">
                    <div class="tb-node-title">{{editingRuleNode.component.name}}</div>
                    <div>&nbsp;</div>
                    <div class="tb-node-description" [innerHtml]="editingRuleNode.component.configurationDescriptor.nodeDefinition.description | safe: 'html'"></div>
                    <div>&nbsp;</div>
                    <div class="tb-node-details" [innerHtml]="editingRuleNode.component.configurationDescriptor.nodeDefinition.details | safe: 'html'"></div>
                  </div>
                </div>
              </mat-tab>
            </mat-tab-group>
          </tb-details-panel>
          <tb-details-panel *ngIf="editingRuleNodeLink" class="flex-1"
                            headerTitle="{{editingRuleNodeLink.label}}"
                            headerSubtitle="{{'rulenode.link-details' | translate}}"
                            [isReadOnly]="false"
                            [isAlwaysEdit]="true"
                            (closeDetails)="onEditRuleNodeLinkClosed()"
                            (toggleDetailsEditMode)="onRevertRuleNodeLinkEdit()"
                            (applyDetails)="saveRuleNodeLink()"
                            [theForm]="tbRuleNodeLink.ruleNodeLinkFormGroup">
            <div class="details-buttons">
              <div [tb-help]="'ruleEngine'"></div>
            </div>
            <tb-rule-node-link
              class="flex-1"
              #tbRuleNodeLink
              [(ngModel)]="editingRuleNodeLink"
              [allowedLabels]="editingRuleNodeLinkLabels"
              [allowCustom]="editingRuleNodeAllowCustomLabels"
              [sourceRuleChainId]="editingRuleNodeSourceRuleChainId">
            </tb-rule-node-link>
          </tb-details-panel>
        </mat-drawer>
        <mat-drawer-content class="tb-rulechain-graph-content">
          <button color="primary"
                  mat-mini-fab
                  class="tb-library-node-btn"
                  (click)="drawer.toggle();"
                  matTooltip="{{ (drawer.opened ? 'rulenode.close-node-library' : 'rulenode.open-node-library') | translate }}"
                  matTooltipPosition="above">
            <mat-icon class="tb-library-node-btn-icon" [class.tb-library-node-btn-icon-toggled]="drawer.opened">chevron_right</mat-icon>
          </button>
          <!-- Edge-only:  merge comment
          <button #versionControlButton
                  *ngIf="!isImport"
                  color="primary"
                  type="button"
                  mat-mini-fab class="version-control-button"
                  (click)="toggleVersionControl($event, versionControlButton)"
                  matTooltip="{{'version-control.version-control' | translate}}"
                  matTooltipPosition="above">
            <mat-icon>history</mat-icon>
          </button>
          -->
          <button type="button"
                  mat-icon-button class="tb-fullscreen-button tb-mat-40"
                  (click)="isFullscreen = !isFullscreen"
                  matTooltip="{{(isFullscreen ? 'fullscreen.exit' : 'fullscreen.expand') | translate}}"
                  matTooltipPosition="above">
            <mat-icon>{{ isFullscreen ? 'fullscreen_exit' : 'fullscreen' }}</mat-icon>
          </button>
          <div class="tb-absolute-fill tb-rulechain-graph" (tbcontextmenu)="openRuleChainContextMenu($event)">
            <div #ruleChainMenuTrigger="matMenuTrigger" style="visibility: hidden; position: fixed"
                 [style.left]="ruleChainMenuPosition.x"
                 [style.top]="ruleChainMenuPosition.y"
                 [matMenuTriggerFor]="ruleChainMenu">
            </div>
            <mat-menu #ruleChainMenu="matMenu" class="tb-rule-chain-context-menu"
                      [overlapTrigger]="true">
              <ng-template matMenuContent let-contextInfo="contextInfo">
                <div class="tb-rule-chain-context-menu-container" (mouseleave)="onRuleChainContextMenuMouseLeave()">
                  <div class="tb-context-menu-header {{contextInfo.headerClass}}">
                    <mat-icon *ngIf="!contextInfo.iconUrl">{{contextInfo.icon}}</mat-icon>
                    <img *ngIf="contextInfo.iconUrl" [src]="contextInfo.iconUrl"/>
                    <div class="flex-1">
                      <div class="tb-context-menu-title">{{contextInfo.title}}</div>
                      <div class="tb-context-menu-subtitle">{{contextInfo.subtitle}}</div>
                    </div>
                  </div>
                  <div *ngFor="let menuItem of contextInfo.menuItems">
                    <mat-divider *ngIf="menuItem.divider"></mat-divider>
                    <button *ngIf="!menuItem.divider"
                            mat-menu-item
                            [disabled]="!menuItem.enabled"
                            (click)="menuItem.action(contextMenuEvent)">
                      <span *ngIf="menuItem.shortcut" class="tb-alt-text"> {{ menuItem.shortcut | keyboardShortcut }}</span>
                      <mat-icon *ngIf="menuItem.icon">{{menuItem.icon}}</mat-icon>
                      <span translate>{{menuItem.value}}</span>
                    </button>
                  </div>
                </div>
              </ng-template>
            </mat-menu>
            <fc-canvas #ruleChainCanvas
                       id="tb-rulchain-canvas"
                       [model]="ruleChainModel"
                       (modelChanged)="onModelChanged()"
                       [selectedObjects]="selectedObjects"
                       [edgeStyle]="flowchartConstants.curvedStyle"
                       [automaticResize]="true"
                       fitModelSizeByDefault="false"
                       [nodeWidth]="170"
                       [nodeHeight]="50"
                       [dragAnimation]="flowchartConstants.dragAnimationRepaint"
                       [userCallbacks]="editCallbacks">
            </fc-canvas>
          </div>
          <section class="tb-footer-buttons flex flex-row flex-wrap items-end justify-start">
            <button [disabled]="isLoading$ | async"
                    mat-fab color="accent" class="tb-btn-footer"
                    [class.tb-hide]="!objectsSelected()"
                    (click)="deleteSelected()"
                    matTooltip="{{ 'rulenode.delete-selected-objects' | translate }}"
                    matTooltipPosition="above">
              <mat-icon>delete</mat-icon>
            </button>
            <button [disabled]="(isLoading$ | async) || !isDebugSettingsEnabled()"
                    mat-fab color="accent" class="tb-btn-footer"
                    (click)="resetDebugSettingsInAllNodes()"
                    matTooltip="{{ 'rulenode.reset-debug-settings' | translate }}"
                    matTooltipPosition="above">
              <mat-icon>bug_report</mat-icon>
            </button>
            <button [disabled]="(isLoading$ | async) || isInvalid || (!isDirtyValue && !isImport)"
                    mat-fab color="accent" class="tb-btn-footer"
                    (click)="saveRuleChain()"
                    matTooltip="{{ 'action.apply-changes' | translate }}"
                    matTooltipPosition="above">
              <mat-icon>done</mat-icon>
            </button>
            <button [disabled]="(isLoading$ | async) || !isDirtyValue"
                    mat-fab color="accent" class="tb-btn-footer"
                    (click)="revertRuleChain()"
                    matTooltip="{{ 'action.decline-changes' | translate }}"
                    matTooltipPosition="above">
              <mat-icon>close</mat-icon>
            </button>
          </section>
        </mat-drawer-content>
      </mat-drawer-container>
    </div>
  </section>
</div>
